<?php
class ControllerArticleTemplate extends ListController {

    protected function heading_title() {
        return "模板列表";
    }
    protected function primary_key_name() {
        return "ID";
    }
    protected function class_path() {
        return "article/template";
    }
    protected function model_name() {
        return "model_article_template";
    }


    protected function get_list_data(&$ret,$filter_data,$url)
    {
        $total = $this->{$this->model_name()}->getCount();

        if (isset($this->request->get['filter_title'])) {
            $filter_data['filter_title'] = $this->request->get['filter_title'];
        } else {
            $filter_data['filter_title']  = null;
        }
        if (isset($this->request->get['filter_category'])) {
            $filter_data['filter_category'] = $this->request->get['filter_category'];
        } else {
            $filter_data['filter_category']  = null;
        }

        $status = 'publish';
        if(isset($_GET['status']) && $_GET['status'] == 'draft')
            $status = 'draft';
        $filter_data['status'] = $status;
        $ret = $this->{$this->model_name()}->getList($filter_data);

        foreach ($ret as &$result) {

            $result['preview'] = $this->url->link('article/article/preview', 'token=' . $this->session->data['token'] . '&'.$this->primary_key_name(). '=' . $result[$this->primary_key_name()] . $url, 'SSL');
        }

        return $total;
    }

    protected function push_data(&$data){
        $this->load->model('article/term');

        $tmp = array();
        $tmp['taxonomy'] = 'category';
        $data['categories'] = $this->model_article_term->getList($tmp);


        if (isset($this->request->get['filter_title'])) {
            $data['filter_title'] = $this->request->get['filter_title'];
        } else {
            $data['filter_title'] = null;
        }


        if (isset($this->request->get['filter_category'])) {
            $data['filter_category'] = (int)$this->request->get['filter_category'];
        } else {
            $data['filter_category'] = null;
        }




        $status = 'publish';
        if(isset($_GET['status']) && $_GET['status'] == 'draft')
            $status = 'draft';

        $data['status'] = $status;
    }


    protected function get_form_data(&$data){
        $item = null;
        if (isset($this->request->get[$this->primary_key_name()]) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
            $item = $this->{$this->model_name()}->getItem($this->request->get[$this->primary_key_name()]);
            $data['post_status'] = $item['post_status'];
        }

        $this->load->model('tool/image');

        if (isset($this->request->post['post_title'])) {
            $data['post_title'] = $this->request->post['post_title'];
        } elseif (isset($item)) {
            $data['post_title'] = $item['post_title'];
        } else {
            $data['post_title'] = '';
        }

        if (isset($this->request->post['image'])) {
            $data['image'] = $this->request->post['image'];
        } elseif (isset($item)) {
            $data['image'] = $item['image'];
        } else {
            $data['image'] = '';
        }

        $data['placeholder'] = $this->model_tool_image->resize('no_image.png', 100, 100);

        if (is_file(DIR_IMAGE . $data['image'])) {
            $data['thumb'] = $this->model_tool_image->resize($data['image'], 100, 100);
        }
        else{
            $data['thumb'] = $data['placeholder'];
        }

        if (isset($this->request->post['post_content'])) {
            $data['post_content'] = $this->request->post['post_content'];
        } elseif (isset($item)) {
            $data['post_content'] = $item['post_content'];
        } else {
            $data['post_content'] = '';
        }


    }




    protected function validateForm() {
        if (!$this->user->hasPermission('modify', $this->class_path())) {
            $this->error['warning'] = $this->language->get('error_permission');
            return !$this->error;
        }

        if (utf8_strlen($_POST['post_title']) == 0) {
            $this->error['warning'] = '标题不能为空';
            return !$this->error;
        }

        return !$this->error;
    }

    protected function validateDelete() {
        if (!$this->user->hasPermission('modify', $this->class_path())) {
            $this->error['warning'] = $this->language->get('error_permission');
            return !$this->error;
        }



        return !$this->error;
    }
}